package com.school.graduation.detailed.model;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.school.graduation.R;
import com.school.graduation.detailed.view.DetailedActivityNew;
import com.school.graduation.detailed.view.ReplyActivity;
import com.school.graduation.model.Detailed;
import com.school.graduation.util.UserUtil;

import java.util.ArrayList;
import java.util.List;

public class DetailedAdapter extends RecyclerView.Adapter<DetailedViewHolder> {
    private Context context;
    private List<Detailed.CommentsBean> comments;
    private boolean isSeller;
    private String sellerId;
    private Detailed detailed;
    private DetailedActivityNew view;
    public DetailedAdapter(Context context,String sellerId){
        this.context = context;
        this.sellerId = sellerId;
        this.comments = new ArrayList<Detailed.CommentsBean>();
        view = (DetailedActivityNew)context;
        if (sellerId != UserUtil.getCurrentUser(context.getApplicationContext()).getId()){
            isSeller = false;
        }
    }
    @Override
    public DetailedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(context).inflate(R.layout.item_detailed_interaction_new,parent,false);
        return new DetailedViewHolder(root);
    }

    @Override
    public void onBindViewHolder(DetailedViewHolder holder, final int position) {
        Detailed.CommentsBean.FromBean fromBean = comments.get(position).getFrom();
        //Log.e("data",fromBean.getName());
        //holder.getName().setText(comments.get(position).getFrom().getName());
        holder.getName().setText(comments.get(position).getFrom().getName());
        holder.getContent().setText(comments.get(position).getContent());
        if (isSeller){
            holder.getReplayButton().setVisibility(View.VISIBLE);
            holder.getReplayButton().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ReplyActivity.class);
                    intent.putExtra("data",comments.get(position));
                    view.startActivity(intent);
                }
            });
        }
        if (comments.get(position).getReply().size() != 0 && comments.get(position).getReply() != null){
            holder.getReplayLayout().setVisibility(View.VISIBLE);
            holder.getReplayName().setText(comments.get(position).getReply().get(0).getFrom().getName());
            holder.getReplayContent().setText(comments.get(position).getReply().get(0).getContent());
        }
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }

    public void initData(List<Detailed.CommentsBean> data){
        this.comments = data;
        notifyDataSetChanged();
    }
}
